import requests
def name(na) :
    return f'https://apis.tianapi.com/{na}/index'
def res(api_key,api_url) :
    dic = {'key': api_key, 'num': 5}
    response = requests.get(api_url, params=dic)
    data = response.json()
    return data
def print_info(data) :
    if data['code'] == 200:
        result = data['result']
        for news in result['newslist']:
            ctime = news.get('ctime', '未知时间')
            title = news.get('title', '未知标题')
            description = news.get('description', '未知描述')
            url = news.get('url', '未知链接')
            print("发布时间:", ctime)
            print("标题:", title)
            print("描述:", description)
            print("链接:", url)
            print('\n')
    else:
        print("API请求失败:", data['msg'])
def main():
    api_key = 'apikey'
    api_url_AI = name('ai')
    api_url_zonghe = name('generalnews')
    api_url_kexue = name('sicprobe')
    api_url_tiyu = name('tiyu')
    api_url_guonei = name('guonei')
    api_url_IT = name('it')
    api_url_jiankang = name('health')
    api_url_guoji = name('world')
    list = [api_url_AI,api_url_zonghe,api_url_kexue,api_url_tiyu,api_url_guonei,api_url_IT,api_url_jiankang,api_url_guoji]
    for i in list :
        data = res(api_key,i)
        print_info(data)
if __name__ == '__main__':
    main()
    input('请按任意键继续...')